<?php
include './control/myFunction.php';
$mysql = include './db/mysql.php';
$config = include './config.php';
//载入配置文件
cc($config);
//验证码错误提示变量
$notice='';	
$notice_text='';
// 获取用户留言时间方法
	function speak_time(){
			date_default_timezone_set('prc');
			$dates = getdate();
			$date['year']      = $dates['year'];
			$date['mon']       = $dates['mon'];
			$date['day']       = $dates['mday'];
			$date['hours']     = $dates['hours'];
			$date['minutes']   = $dates['minutes'];
			$date['seconds']   = $dates['seconds'];
			return $date;
		}
//c函数练习写一遍
	function cc($var=null,$value=null){
		static $con = array();
		// 如果是数组设置配置
		if(is_array($var)){	
			$con = array_merge($con,$var);
			return;
		}
		//如果为空就是查看配置文件
		if(empty($var)){
			return $con;
		}
		// 如果为一个字符串是查看配置项
		if(is_string($var)){
			if($value==null){
				return $con[$var]=isset($con[$var])?$con[$var]:null;;
			}else{
				// 如果是两个字符串是设置配置项
				$con[$var]=isset($con[$var])?$value:null;
				return;
			}
		}
	}
//制造验证码方法
		function yzm(){
			 $code = md5(microtime(true));
			 $str = mt_rand(0,26);
			 //读配置项获得验证码长度
			 $yzm_len = cc('yzm_len');
			 // 制造验证码
			 $yzm = substr($code,$str,$yzm_len);
			 //加入cookie，作比较用
			 setcookie('yzm',$yzm);
			 return $yzm;
		}
// 主逻辑部分
//判断是否填写留言
if(!empty($_POST)){
	//获取留言时间
	$time = speak_time();
	if(isset($_POST['Id'])){
		$mysql[$_POST['Id']]['text']=$_POST['text'];
		$mysql[$_POST['Id']]['time']=$time;
		$rs_c = save_to_db($mysql);	
		if($rs_c){
					$mes = "修改成功";
					$url = "index.php";
					promt($mes);
				}else{
					$mes="修改失败";
					promt($mes);
				}	
	}else{
		if($_POST['yzm']==$_COOKIE['yzm']){
			$notice = "成功";
			if($_POST['text']!='写点什么吧'){
				// 加入评论时间
				$_POST['time']=$time;
				
				if(count($mysql)){
					array_push($mysql,$_POST);
				}else{
						
						$mysql[]=$_POST;	
						
					}								
				// 调用存入数组函数
				$rs = save_to_db($mysql);
				if($rs){
					$mes = "添加成功";
					$url = "index.php";
					promt($mes);
				}else{
					$mes="添加失败";
					promt($mes);
				}	
			}else{
				$notice_text = "没写东西也敢提交 %>_<%";
				}
		}else{
				$notice = "验证码错误";
			}
	}	

	
}

//判断get，如果get传了参数，进行分析
if(!empty($_GET)){
	$Id =  $_GET['id'];
	$action = $_GET['action'];
	if($action=='delete'){
		unset($mysql[$Id]);
		$mysql = array_values($mysql);
		//写入数据库
		$rss = save_to_db($mysql);
		if($rss){
					$mes = "删除成功";
					$url = "index.php";
					promt($mes);
				}else{
					$mes="删除失败";
					promt($mes);
				}	
	}
	if($action=='change'){
		$js = "<script type='text/javascript'>
				window.onload=function(){
					//alert('hah');
					document.getElementById('caver').style.height=document.height;
					document.getElementById('change_show').style.display='block';
					document.getElementById('caver').style.display='block';
				}				
				</script>";
		echo $js;
		$mes_change = $mysql[$Id];

	}
	

}
		